package com.easybuy.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easybuy.sys.model.SysUser;
import com.easybuy.sys.service.SysStoreMessageService;
import com.easybuy.sys.service.SysUserService;
import com.easybuy.utils.RandomNumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author SR.Y
 * @since 2020-12-05
 */
@RestController
@RequestMapping("/model/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysStoreMessageService sysStoreMessageService;


    /**
     * 用户注册
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public boolean createUser(@RequestBody SysUser user) {
        if (user.getPwd() == null) {
            user.setPwd("000000");
        }
        String md5Password = DigestUtils.md5DigestAsHex(user.getPwd().getBytes());
        user.setPwd(md5Password);
        user.setCreattime(new Date());
        return sysUserService.save(user);
    }


    /**
     * 用户登录
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public SysUser loginUser(@RequestBody SysUser user, String rand, HttpServletRequest request) {
        System.out.println(user.getUname() + rand + "================");
        String arandom = (String) (request.getSession().getAttribute("random"));
        if (!(arandom.equalsIgnoreCase(rand))) {
            return null;
        }
        if (2 == user.getState()) {
            if (!sysStoreMessageService.getState(user.getId())) {
                return null;
            }
        }
        return sysUserService.login(user);
    }

    /**
     * 修改用户
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public boolean update(@PathVariable Integer id, @RequestBody SysUser user) {
        user.setId(id);
        return sysUserService.update(user);
    }
    /**
     * 删除用户
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public boolean delete(@RequestBody List<Integer> idList) {
        return sysUserService.delete(idList);
    }

//    /**
//     * 用户查询
//     */
//    @RequestMapping(value = "/list", method = RequestMethod.GET)
//    @ResponseBody
//    public Page<SysUser> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
//                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
//
//        return sysUserService.list(pageSize, pageNum);
//
//    }
//    }

    /**
     * 用户条件查询
     * @param user
     * @param pageSize
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Page<SysUser> list(@RequestBody SysUser user,
                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
//        if(user!=null){
            return sysUserService.list(user,pageSize, pageNum);
//        }else{
//            return sysUserService.list(pageSize, pageNum);
//        }

    }

    /**
     * 用户查询
     * @param pageSize
     * @param pageNum
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Page<SysUser> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
//        if(user!=null){
        return sysUserService.list(pageSize, pageNum);
//        }else{
//            return sysUserService.list(pageSize, pageNum);
//        }

    }

    /**
     * 通过id查询
     */
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ResponseBody
    public SysUser getOneById(@PathVariable Integer id) {
        return sysUserService.getOneById(id);
    }

    /**
     * 生成验证码
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping(value = "/random", method = RequestMethod.GET)
    public String InitRandom(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        //	PrintWriter out = response.getWriter();

        RandomNumUtil rdnu = RandomNumUtil.Instance();
        System.out.println(rdnu.getStr());
        //this.setInputStream(rdnu.getImage());
        //生成验证码 后  并且保存
        request.getSession().setAttribute("random", rdnu.getStr());
        //return "success";
        BufferedInputStream bis = null;
        OutputStream os = null;
        bis = new BufferedInputStream(rdnu.getImage());
        byte[] buffer = new byte[512];
        response.setCharacterEncoding("UTF-8");
        //不同类型的文件对应不同的MIME类型
        response.setContentType("image/*");
        //文件以流的方式发送到客户端浏览器
        response.setContentLength(bis.available());

        os = response.getOutputStream();
        int n;
        while ((n = bis.read(buffer)) != -1) {
            os.write(buffer, 0, n);
        }
        bis.close();
        os.flush();
        os.close();
        return rdnu.getStr();
        //out.flush();
        //out.close();
    }
}

